package com.ydy.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.jdbc.Field;

import com.ydy.domain.Emp;
import com.ydy.domain.Student;
//import com.ydy.service.AddrService;
import com.ydy.vo.Studentvo;
import jdk.nashorn.internal.runtime.JSONFunctions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.swing.*;
import javax.xml.soap.Name;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@RequiredArgsConstructor
public class StudentBizImpl implements StudentBiz {

    @Autowired
    private StudentService studentService;
    @Autowired
    private EmpService empService;

    private final StringRedisTemplate stringRedisTemplate;



    @Override
    public void insertStudent(Student student) {

    }

    @Override
    public void updateStudentByName(Student student) {
        if(student.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        Student byId = studentService.getById(student.getId());
        if(byId!= null){
            byId.setName(student.getName());
            studentService.updateById(byId);
        }
    }

    @Override
    public void deleteById(Integer id) {
        studentService.removeById(id);
    }

    @Override
    public Studentvo getStudentvo(Integer id) {
      Student student = studentService .getById(id);
      if (student == null){
          throw new RuntimeException("用户不存在");
      }
      Studentvo studentvo = new Studentvo();
      studentvo.setId(student.getId());
      studentvo.setName(student.getName());

        Emp emp = empService.getOne(new LambdaQueryWrapper<Emp>().eq(Emp::getStuId,id).last("limit 1"));
        if (emp !=null){
            studentvo.setEmp(emp);
        }
        return studentvo;
    }

    @Override
@Transactional(rollbackFor =  Exception.class)
    public void addStudent(Studentvo studentvo) {
        Student student = new Student();
        student.setName(studentvo.getName());
        studentService.save(student);
        log.info("stu id:{}",student.getId());
        Emp emp = new Emp();
        emp.setId(studentvo.getId());
        emp.setName(studentvo.getName());
        emp.setGender(studentvo.getGender());
        emp.setSalary(studentvo.getSalary());
        emp.setDeptId(studentvo.getDeptId());
        emp.setJoinDate(studentvo.getJoinDate());
        emp.setStuId(student.getId());
        empService.save(emp);
        log.info("studentvo:{}新增成功",studentvo);


    }
  public static final String STUDENT_KEY ="student:";
    @Override
    public Studentvo getSrudentvowithRedis(String name) {
        String s = stringRedisTemplate.opsForValue().get(STUDENT_KEY + name);
        if (!StringUtils.isEmpty(s)){
            Studentvo studentvo = JSON.parseObject(s,Studentvo.class);
            return  studentvo;
        }
        Studentvo studentvo = this.getSrudentvowithAccount(name);
        stringRedisTemplate .opsForValue().set (STUDENT_KEY+name,JSON.toJSONString(studentvo),10, TimeUnit.SECONDS);
        try{
            Thread.sleep(1000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return studentvo;


    }

    @Override
    public boolean checkName(String name, String password) {
        Studentvo studentvoWithName = this.getSrudentvowithRedis(name);
        if (null == studentvoWithName){
            return  false;
        }
        if (studentvoWithName.getPassword().equals(password)){
            return  true;
        }
        return false;
    }

    @Override
    public Studentvo getSrudentvowithAccount(String name) {
      Studentvo studentvo  = new Studentvo();
      Emp one = empService.getOne(new LambdaQueryWrapper<Emp>().eq(Emp::getName,name).last("limit 1"));
        if (one !=null){
            studentvo.setEmp(one);
        }
        Student student = studentService.getById(one.getStuId());
        studentvo.setId(student.getId());
        studentvo.setName(student.getName());
        studentvo.setPassword(student.getPassword());

        Emp emp = empService.getOne(new LambdaQueryWrapper<Emp>().eq(Emp::getStuId,student.getId()).last("limit 1"));
        if (emp !=null){
            studentvo.setEmp(emp);
        }
        return studentvo;
    }
}
